package queue

import (
	"encoding/json"
	"gin/validate"
	"github.com/RichardKnop/machinery/v2/tasks"
)

var (
	asyncTaskMap map[string]interface{}
)

// 方法名
const (
	TaskName = "MediaProcessing"
)

// 执行媒体处理
func MediaProcessing(args ...[]byte) (err error) {
	for _, arg := range args {
		Param := validate.TranscodeVideoParam{}
		err = json.Unmarshal(arg, &Param)
		if err == nil {
			err = handleTranscode(Param)
		}
	}
	return
}

// SendMediaProcessingTask 调用点调用此异步任务函数
func SendMediaProcessingTask(Param []byte) (err error) {
	args := []tasks.Arg{
		{
			Type:  "[]byte",
			Value: Param,
		},
	}
	task, _ := tasks.NewSignature(TaskName, args)
	task.RetryCount = 3
	_, err = AsyncTaskCenter.SendTask(task)

	return
}

func initAsyncTaskMap() {
	asyncTaskMap = make(map[string]interface{})
	asyncTaskMap[TaskName] = MediaProcessing
}
